package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.User;
import com.sky.entity.Userc;
import com.sky.result.Result;
import com.sky.service.WXservice;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/user")
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserController {
    @org.springframework.beans.factory.annotation.Autowired
    private com.sky.service.WXservice wxservice;

    @org.springframework.beans.factory.annotation.Autowired
    private com.sky.properties.JwtProperties jwtProperties;

    @PostMapping("/login/{code}")
    @ApiOperation("微信登录")
    public com.sky.result.Result<com.sky.vo.UserLoginVO> login(@PathVariable String code) {
        log.info("微信登录，code：{}", code);
        User user = wxservice.login(code);

        // 生成用户端JWT
        java.util.Map<String, Object> claims = new java.util.HashMap<>();
        claims.put("userId", user.getId());
        String token = com.sky.utils.JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims
        );

        UserLoginVO vo = com.sky.vo.UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();

        return com.sky.result.Result.success(vo);
    }
    @PostMapping("logout")
    @ApiOperation("退出登录")
    public com.sky.result.Result logout() {
        log.info("用户：{} 退出登录", BaseContext.getCurrentId());
        return Result.success();
    }
}
